<?php
namespace Admin\Models\res;

use Phalcon\Di;
use Helpers\CurlClient as HttpClient;
use Admin\Models\InterfaceService;

/**
 * Class Dictionaries 字典
 * @package Admin\Models\Question
 */
class Dictionaries extends InterfaceService
{

    private static $interfaceKey = 'questionBaseUrl';
    private static $subjectArts    = array(1,3,5,6,10,11); //文科科目
    private static $subjectScience = array(2,4,7,8,9,12);  //理科科目
    private static $artsQuestionUserType    = array(1,2,3,4,5,7,9,10); //文科题型
    private static $scienceQuestionUserType    = array(1,2,3,4,6,8); //理科题型

    const GET_DICTIONARIES_URL = '/api/v1/dictionaries';
    const GET_STAGE_GRADES = "/api/v1/stageGrades";
    const GET_DICTIONARIES_SUBJECTID_URL = '/api/v1/dictionaries/{subjectId}';

    /**
     * #方法说明：题库数据层
     * # Created by zsc
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * #方法说明：创建httpUrl
     * @param $httpUrl
     * @param array $paramArr
    # Created by zsc
     */
    public static function createHttpUrl($httpUrl,$paramArr=array()){
        return parent::createInterfaceUrl($httpUrl,$paramArr,self::$interfaceKey);
    }


    /**
     * #方法说明：将属性数据转换为名称
     * @param array $data
     * @return string
    # Created by zsc
     */
    public function convertLibraryName($data=array(),$separate=false){
        $dictionariesArr = $this->runDictionariesData();
        if($separate){
            $result = array();
            if($data['subjectId']) $result['subject'] = ($dictionariesArr['subject'][$data['subjectId']]);
            if($data['gradeId']) $result['grade'] =$dictionariesArr['grade'][$data['gradeId']];
            if($data['textBookVersion']) $result['textBookVersion'] = $dictionariesArr['textBookVersion'][$data['textBookVersion']];
        }else{
            $result = '';
            if($data['subjectId']) $result.=($dictionariesArr['subject'][$data['subjectId']].'·');
            if($data['gradeId']) $result.=$dictionariesArr['grade'][$data['gradeId']];
            if($data['textBookVersion']) $result.= ('('.$dictionariesArr['textBookVersion'][$data['textBookVersion']].')');
        }
        return $result;
    }


    /**
     * #方法说明：将字典的键值转换为字符串
     * @param array $data 数据id
     * @param null $type  字典字段类型
     * @return null
    # Created by zsc
     */
    public function convertDictionariesString($data=array(),$type=null){
        if(!$data || !$type)return null;
        if(is_array($data)==false)$data = explode(',',$data);
        $dictionariesArr = $this->runDictionariesData();
        $resultArr = array();
        if($data)foreach ($data as $key => $value){
            $resultArr[] = $dictionariesArr[$type][$value];
        }
        return $resultArr?implode(',',$resultArr):null;

    }



    /**
     * #方法说明：使用人的字典权限数据请求获取
     * # Created by zsc
     */
    public function userDictionariesJurisdiction(){
        $userId = Di::getDefault()->getShared('session')->get('UserId');
        $userIdJurisdiction = array( //模拟请求的字典权限
            'grade' => [71,72,81,82,91,92,93],
            'subject' => [1,2,3,4],
            'textBookVersion' => [1,2,3,4],
        );
        return $userIdJurisdiction;
    }

    /**
     * #方法说明：获取用户字典权限
     * # Created by zsc
     */
    public function userDictionaries(){
        $userIdJurisdiction  = self::userDictionariesJurisdiction();
        $dictionariesArr = $this->runDictionariesData();
        if($dictionariesArr['grade'])foreach ($dictionariesArr['grade'] as $key => $value){
            if($userIdJurisdiction['grade'] && is_array($userIdJurisdiction['grade']) && in_array($key,$userIdJurisdiction['grade'])==false){
                unset($dictionariesArr['grade'][$key]);
            }
        }
        if($dictionariesArr['subject'])foreach ($dictionariesArr['subject'] as $key => $value){
            if($userIdJurisdiction['subject'] && is_array($userIdJurisdiction['subject']) && in_array($key,$userIdJurisdiction['subject'])==false){
                unset($dictionariesArr['subject'][$key]);
            }
        }
        if($dictionariesArr['textBookVersion'])foreach ($dictionariesArr['textBookVersion'] as $key => $value){
            if($userIdJurisdiction['textBookVersion'] && is_array($userIdJurisdiction['textBookVersion']) && in_array($key,$userIdJurisdiction['textBookVersion'])==false){
                unset($dictionariesArr['textBookVersion'][$key]);
            }
        }
        return $dictionariesArr;
    }

    /**
     * #方法说明：年级对应的学段
     * @return array
    # Created by zsc
     */
    public static function getStageGrades(){
        $data = array();
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_STAGE_GRADES));
        $result = $http->get()->bodyConvert();
        if($result)foreach($result as $key => $value){
            $data[$value['gradeId']] = $value['stageId'];
        }
        return $data;
    }


    /**
     * #方法说明：获取字典数据
     * @return array
    # Created by zsc
     */
    public static function runDictionaries(){
        $data = array();
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_DICTIONARIES_URL));
        $result = $http->get()->bodyConvert();
        if($result)foreach($result as $key => $value){
            $data[$value['paramType']][$value['id']] = $value['name'];
        }
        return $data;
    }




    /**
     * #方法说明：获取缓存数据
     * @return array|bool
    # Created by zsc
     */
    public function runDictionariesData($force=false){
        $dcacheKey = 'dictionaries';
       // $data = parent::cache()->get($dcacheKey);
        //if(!$data || $force===true){
            $data = self::runDictionaries();
          //  parent::cache()->save($dcacheKey,$data,600);
        //}
        return $data;
    }

    /**
     * #方法说明：区分文科和理科
     * @param $subjectId
    # Created by zsc
     *
     *     [subject] => Array
    (
    [1] => 语文
    [2] => 数学
    [3] => 英语
    [4] => 科学
    [5] => 历史
    [6] => 思想品德(道德与法制)
    )
     *
     */
    public static function subjectType($subjectId=null){
        if(in_array($subjectId,self::$subjectArts))return 1;//文科
        if(in_array($subjectId,self::$subjectScience))return 2;//理科
    }


    /**
     * #方法说明：科目所对应的题型
     * # Created by zsc
     */
    public static function subjectQuestionUserType($subjectId){
         $result = self::subjectType($subjectId);
         if($result==1)return self::$artsQuestionUserType;
        if($result==2)return self::$scienceQuestionUserType;
        return [];
    }


    /**
     * #方法说明：判断是否是英语
     * @param $subjectId
    # Created by zsc
     */
    public static function isEnglish($subjectId=null){
        if(in_array($subjectId,array(3)))return true;
        return false;
    }

    /**
     * #方法说明：返回KYE对应的学段值
     * @return array
    # Created by zsc
     */
    public static function runStageString(){
        return array(
            'UNKNOWN' => array( 'id'=>0, 'name'=>'未知'),
            'PRIMARY' => array( 'id'=>1, 'name'=>'小学'),
            'JUNIOR' =>  array( 'id'=>1, 'name'=>'初中'),
            'HIGH' =>    array( 'id'=>3, 'name'=>'高中'),
        );
    }


    /**
     * #方法说明：难度分类
     * @return array
    # Created by zsc
     */

    public static function runDifficulty(){
        return [
            0 =>  '难度',
            1  => '入门',
            2  => '中级',
            3  => '高级',
        ];
    }


}